#pragma once

#if defined (__linux)
#include <jsoncpp/json/json.h>
#elif defined (__APPLE__)
#include <json/json.h>
#endif

namespace atm_server
{
    class except : public std::exception
    {
    public:
        except(const std::string& msg) : s("atm_server " + msg)
        {
        }

        const char* what() const throw()
        {
            return s.c_str();
        }

        std::string s;
    };

    struct json_util
    {
        static Json::Value deserialize(const std::string& str)
        {
            Json::Value root;
            std::unique_ptr<Json::CharReader> cr(Json::CharReaderBuilder().newCharReader());

            std::string errs;
            if (!cr->parse(str.c_str(), str.c_str() + str.size(), &root, &errs))
                throw except("deserialize parse error " + errs);

            return root;
        }

        static std::string serialize(const Json::Value& root)
        {
            std::unique_ptr<Json::StreamWriter> sw(Json::StreamWriterBuilder().newStreamWriter());
            std::stringstream res;

            if (sw->write(root, &res) != 0)
                throw except("serialize write error");

            return res.str();
        }
    };


}
